<template>
	<a-modal title="出库单详情" :width="1300" :visible="visible" :confirmLoading="confirmLoading" :footer="null"
		@ok="handleSubmit" @cancel="handleCancel">
		<a-card :bordered="false">
			<div class="table-page-search-wrapper">
				<a-form layout="inline">
					<a-row :gutter="48">

						<a-col :md="8" :sm="24" v-if="false">
							<a-form-item label="订单ID">
								<a-input v-model="queryParam.orderId" placeholder="请输入订单ID" />
							</a-form-item>
						</a-col>

					</a-row>
				</a-form>
			</div>

			<s-table ref="table" size="default" :columns="columns" :data="loadData" :alert="true" :showPagination="false">

				<template slot="normalNumber" slot-scope="text, record">
					<a-input-number v-model:value="record.normalNumber" :step="1" :min="0" style="width: 67px;" />
				</template>
				<template slot="materialModelNumber" slot-scope="text, record">
					{{ record.material ? record.material.materialModelNumber : '' }}
				</template>
				<!-- <span slot="image" slot-scope="text">
					<image-preview :images="text" />
				</span> -->
				<span slot="image" slot-scope="text,record">
					<image-preview1 :images="record.material.image" :imageUrl="record.material.imageUrl" />
				</span>
				<template slot="extraNumber" slot-scope="text, record">
					<a-input-number v-model:value="record.extraNumber" :step="1" :min="0" style="width: 67px;" />
				</template>
				<span slot="action" slot-scope="text, record">
					<a @click="$refs.outTimelineForm.show(record)">出库记录(包材未优化)</a>
				</span>
			</s-table>

			<add-form ref="addForm" @ok="handleOk" />
			<out-timeline-form ref="outTimelineForm" />
		</a-card>
	</a-modal>
</template>

<script>
import { STable } from '@/components'
import { page } from '@/api/modular/main/emsordermaterial/emsOrderMaterialManage'
import addForm from '@/views/main/emsordermaterial/emsOrderMaterialAddFormOutOrder'
import editForm from '@/views/main/emsordermaterial/emsOrderMaterialEditForm'
import outTimelineForm from '@/views/main/emsordermaterial/emsOrderMaterialOutTimelineForm'
import materialForm from '@/views/main/emsmaterial/emsMaterialIndex'
import emsMaterialIndex1 from '@/views/main/emsmaterial/emsMaterialIndex1'
import productAddForm from '@/views/main/emsorder/emsOrderProductAddForm'
import productEditForm from '@/views/main/emsorder/emsOrderProductEditForm'
import { MATERIAL_TYPE_ENUM } from '@/utils/util'

export default {
	components: {
		STable,
		addForm,
		editForm,
		materialForm,
		emsMaterialIndex1,
		productAddForm,
		productEditForm,
		outTimelineForm,
	},
	props: {
		readonly: false,
		materialType: {
			type: [String, Number],
			default: MATERIAL_TYPE_ENUM.material.id
		}
	},
	data() {
		return {
			title: null,
			advanced: false, // 高级搜索 展开/关闭
			queryParam: {},  // 查询参数
			viewState: '',
			columns: [
				{
					title: '产品',
					dataIndex: 'productName',
				},
				{
					title: 'BOM',
					customRender: (text, record, index) => record.bomCode ? `${record.bomCode}(${record.bomName})` : `/`
				},
				{
					title: '物料',
					dataIndex: 'materialName'
				},
				{
					title: '规格',
					dataIndex: 'materialModelNumber',
					scopedSlots: { customRender: 'materialModelNumber' }
				},
				{
					title: '图片',
					dataIndex: 'image',
					scopedSlots: { customRender: 'image' }
				},
				{
					title: '物号',
					dataIndex: 'materialCode'
				},
				{
					title: '正常出库数量',
					dataIndex: 'number'
				},
				{
					title: '额外出库数量',
					dataIndex: 'extraNumber'
				},
			],
			// 加载数据方法 必须为 Promise 对象
			loadData: parameter => {
				this.queryParam.orderId = this.emsOrder.orderId
				this.queryParam.materialType = this.materialType
				this.queryParam.pageSize = -1
				return page(Object.assign(parameter, this.queryParam)).then((res) => {
					res.data.rows.forEach(value => {
						value.normalNumber = 0
					})
					return res.data
				})
			},
			selectedRowKeys: [],
			selectedRows: [],
			defaultExpandedKeys: [],
			replaceFields: {
				key: 'id',
			},
			visible: false,
			confirmLoading: false,
			emsOrder: null,
			extra: null,
		}
	},
	watch: {
		emsOrder() {
			setTimeout(function () {
				this.$refs.table.refresh(true)
			}, 100)
		}
	},
	methods: {
		show(record) {
			this.visible = true
			this.emsOrder = record
			setTimeout(() => {
				this.handleOk()
			}, 100)
		},
		toggleAdvanced() {
			this.advanced = !this.advanced
		},
		handleOk() {
			this.$refs.table.refresh()
		},
		onSelectChange(selectedRowKeys, selectedRows) {
			this.selectedRowKeys = selectedRowKeys
			this.selectedRows = selectedRows
		},
		handleSubmit() {
		},
		handleCancel() {
			this.visible = false
		},
	}
}
</script>